<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

//$this->load->helper('html');
/**
 *
 * Get localization Lang String from config/localizations.php
 * @author  Liang Dong
 *
 * @param   string  name
 * @return  string
 *
 */
if ( ! function_exists('getLang')) {

    function getLang($name){
        global $_localizations;

        if(!is_array($_localizations)){
            if(defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/localizations.php')){
                include(APPPATH.'config/'.ENVIRONMENT.'/localizations.php');
            } else if(is_file(APPPATH.'config/localizations.php')){
                include(APPPATH.'config/localizations.php');
            }

            if(!is_array($_localizations)){
               return '';
            }
        }

        if(isset($_localizations[$name])){
            return $_localizations[$name];
        } else {
            return '';
        }
    }
}

/**
 *
 * Include title into HTML
 * @author  Liang Dong
 *
 * @return  void
 *
 */
if ( ! function_exists('include_title')) {
    function include_title() {
        echo '<title>'.getLang('title').'</title>';
    }
}

/**
 *
 * Include stylesheet
 * @author  Liang Dong
 *
 * @return  null
 *
 */
if ( ! function_exists('include_stylesheets')) {
    function include_stylesheets() {
        // TODO: include stylesheets into  link_tag
        // echo link_tag(/*css_file_path*/);
        $ci = &get_instance();
        $css_array = $ci->config->item('css');
        $css_includes = '';
        foreach($css_array as $para) {
            $css_includes .= link_tag($para);
        }
        echo $css_includes;
    }
}

/**
 *
 * Include javascripts
 * @author  Liang Dong
 *
 * @return  null
 *
 */
if ( ! function_exists('include_javascripts')) {
    function include_javascripts() {
        echo <<<PRINTED
<script type="text/javascript">
function loadJs(path) {
    var script = document.createElement("script"),
        first;
    script.type = "text/javascript";
    script.src = path;
    first = document.getElementsByTagName('script')[0];
    first.parentNode.insertBefore(script, first);
}
PRINTED;

        //TODO
        $ci = &get_instance();
        $js_array = $ci->config->item('javascript');
        foreach($js_array as $para){
            echo "loadJs(\"$para\");";
        }
        echo "</script>";
    }
}

